<?php
/*
    [GRIRMS] (C) 2010 Grandison & JM Technologies

    @author kevin<hasson.lai@mobile-grandison.com>
    @version $Id: datatable.class.php 1 2010-09-15 $
*/


class zz_datatable{
	private $data = array();							//数据数组		
	private $cols = array();							//传入列
	private $calls = array();							//
	private $caption = 'datatable title';				//传入标题
	private $url = '';									//传入url
	private $operation = '';							//传入操作
	private $page = 0;									//传入页数
	private $pagesize = 0;								//
	private $records = -1;
	private $pager = '#pager';
	private $titles = array();
	private $columns = array();
	private $property = array(
		'width' => null,
		'sort' => null,
		'align' => null,
		'valign' => null,
		'key' => null,
		'tpl' => null
	);
	
	public function __construct($page = 1, $records = -1, $data = array()){
		$this->set_page($page);
		$this->set_records($records);
		$this->set_data($data);
		
	}
	
	public function get_count($total_nums){
		global $rows, $page;
		$page_size = intval($rows) > 0 ? $rows : 20;
		$total_pages = ceil($total_nums / $page_size);
		$page = intval($page) < 1 ? 1 : intval($page);
		$page > $total_pages && $page = $total_pages;
		$start_nums = $page_size * ($page - 1);
		if($start_nums < 0) $start_nums = 0;
		return array($page, $total_pages, $start_nums, $page_size);
	}
	
	public function add_col($name, $call = null){
		$this->cols[$name] = $call;
		return $this;
	}
	
	public function add_call($name, $call){
		$this->calls[$name] = $call;
		return $this;
	}
	
	public function set_data($data){
		$this->data = $data;
		if($this->records == -1)  $this->records = count($data);
		return $this;
	}
	
	public function to_json(){
		$data = array();
		foreach($this->data as $row){
			if(count($this->cols) > 0){
				$_row = array();
				foreach($this->cols as $k => $c){
					$_row[$k] = $c == null ? $row[$k] : $c($row[$k], $row);
				}
				$data[] = $_row;
			}else{
				foreach($row as $k => $v){
					if(array_key_exists($k, $this->calls)){
						$call_func = $this->calls[$k];
						$v = $call_func($v, $row);
						$row[$k] = $v ? $v : '';
					}
				}
				$data[] = $row;
			}
			
		}
		$out = array('page' => $this->page, 'records' => $this->records, 'data' => $data);
		header("Expires:Sat, 1 Jan 2000 08:00:00 GMT ");
		header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT ");
		header("Cache-Control: no-cache, must-revalidate ");
		header("Pragma: no-cache ");
		return array2json($out, true);
	}
	//获取数据 end
	
	//组织表格 start
	public function add_column($name, $title = null, $property = array()){
		$title = $title == null ? $name : $title;
		if(array_key_exists('tpl', $property)){
			$property['tpl'] = str_replace('\'', '\\\'', $property['tpl']);
		}
		$column = array(
			'name' => $name,
			'title' => $title,
			'property' => array_merge($this->property, $property)
		);
		$this->titles[] = $title;
		$this->columns[] = $column;
		return $this;
	}

	public function show(){
		$caption = $this->caption;
		$operation = str_replace("'", "\\'", $this->operation);
		$url = $this->url;
		$titles = count($this->titles) > 0 ? "['".implode("', '",$this->titles)."']" : '[]';
		$columns = "[";
		$column_comma = '';
		foreach($this->columns as $column){
			$columns .= "$column_comma\n		             {";
			$columns .= "name:'{$column['name']}'";
			foreach($column['property'] as $k => $v){
				if($v != null) $columns .= ", $k:'$v'";
			}
			$columns .= "}";
			$column_comma = ',';
		}
		$columns .= "\n		          ]";
		//$columns = array2json($this->columns);
		return "
<script type=\"text/javascript\">
$(document).ready(function(){
    $('#datagrid').dataTable({
        url: '$url',
		colNames : $titles,
		columns: $columns,
		rownum : '$this->pagesize',
		page : '$this->page',
		pager : '$this->pager',
		caption : '$caption'
	});
});
</script>";
	}
	//组织表格 end
	
	private function dbug($msg,$line){
		$self = basename(__FILE__);
		exit("<p>file:[{$self}],line:[{$line}],msg:[{$msg}]</p>");
	}

	public function __toString(){
		return $this->show();
	}
	
	public function __call($func, $params){
		
		if(preg_match('/^set_(\w+)$/', $func, $match) && !empty($match[1]) && count($params) >= 1){
			switch($match[1]){
				case 'caption':
					$this->caption = $params[0];
					break;
				case 'url':
					$this->url = $params[0];
					break;
				case 'pagesize':
					$this->pagesize = $params[0];
					break;
				case 'page':
					$this->page = intval($params[0]);
					break;
				case 'records':
					$this->records = intval($params[0]);
					break;
				case 'pager':
					$this->pager = $params[0];
					break;
				case 'operation':
					$this->operation = $params[0];
					break;
				default:
				
					break;
			}
		}
		return $this;
	}

}
?>
